package com.baidumap.administrator.happystudy.ui;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.base.JsonTools;
import com.baidumap.administrator.happystudy.base.Utils;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.SupermarketHomeAdapter;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.Map;

public class RestaurantHomeTab1Activity extends Activity implements View.OnClickListener, SwipyRefreshLayout.OnRefreshListener{

    private int gray = 0xFF7d7d7d;// 灰色
    private int green = 0xFF20bf1e;

    private RelativeLayout mMoreServiceBtn[] = new RelativeLayout[10];
    private View mMoreServiceView[] = new View[10];
    private TextView mMoreServiceTV[] = new TextView[10];
    private RelativeLayout bottom_rlay;


    public ImageLoader loader;
    private ListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private SupermarketHomeAdapter mAdapter;
    private static int pageNo = 1;
    private ArrayList<Map<String, Object>> arrayList;
    private SwipyRefreshLayout mSwipyRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restaurant_home_tab1);
        IniVew();
        iniListView();
    }

    private void iniListView() {
        mSwipyRefreshLayout = (SwipyRefreshLayout) findViewById(R.id.main_SwipyRefreshLayout);
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(pageNo);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
        // LoadImage初始化
        loader = ImageLoader.getInstance();
        pageNo = 1;
        mListView = (ListView) findViewById(R.id.list);
        mAdapter = new SupermarketHomeAdapter(RestaurantHomeTab1Activity.this, loader);
        new MyTask().execute("", "" + pageNo);// 线程加载json数据
    }


    class MyTask extends
            AsyncTask<String, Void, ArrayList<Map<String, Object>>> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected ArrayList<Map<String, Object>> doInBackground(
                String... params) {

            ArrayList<Map<String, Object>> mList = null;
            mList = (ArrayList<Map<String, Object>>) JsonTools.listKeyMaps(
                    "list", Utils.HOUSE_JSON);
            return mList;
        }

        protected void onPostExecute(ArrayList<Map<String, Object>> result) {
            // TODO Auto-generated method stub

            list.addAll(result);//

            mAdapter.bindData(list);
            if (pageNo == 1) {

                mListView.setAdapter(mAdapter);
            }
            pageNo++;
            mAdapter.notifyDataSetChanged();
            super.onPostExecute(result);

        }
    }

    private void IniVew() {
        bottom_rlay =(RelativeLayout) findViewById(R.id.bottom_rlay);

        mMoreServiceBtn[0] = (RelativeLayout) findViewById(R.id.more_service_btn1);
        mMoreServiceBtn[0].setOnClickListener(this);
        mMoreServiceBtn[1] = (RelativeLayout) findViewById(R.id.more_service_btn2);
        mMoreServiceBtn[1].setOnClickListener(this);
        mMoreServiceBtn[2] = (RelativeLayout) findViewById(R.id.more_service_btn3);
        mMoreServiceBtn[2].setOnClickListener(this);

        mMoreServiceBtn[3] = (RelativeLayout) findViewById(R.id.more_service_btn4);
        mMoreServiceBtn[3].setOnClickListener(this);
        mMoreServiceBtn[4] = (RelativeLayout) findViewById(R.id.more_service_btn5);
        mMoreServiceBtn[4].setOnClickListener(this);
        mMoreServiceBtn[5] = (RelativeLayout) findViewById(R.id.more_service_btn6);
        mMoreServiceBtn[5].setOnClickListener(this);

        mMoreServiceBtn[6] = (RelativeLayout) findViewById(R.id.more_service_btn7);
        mMoreServiceBtn[6].setOnClickListener(this);
        mMoreServiceBtn[7] = (RelativeLayout) findViewById(R.id.more_service_btn8);
        mMoreServiceBtn[7].setOnClickListener(this);
        mMoreServiceBtn[8] = (RelativeLayout) findViewById(R.id.more_service_btn9);
        mMoreServiceBtn[8].setOnClickListener(this);
        mMoreServiceBtn[9] = (RelativeLayout) findViewById(R.id.more_service_btn10);
        mMoreServiceBtn[9].setOnClickListener(this);

        mMoreServiceTV[0] = (TextView) findViewById(R.id.more_service_tv1);
        mMoreServiceTV[1] = (TextView) findViewById(R.id.more_service_tv2);
        mMoreServiceTV[2] = (TextView) findViewById(R.id.more_service_tv3);
        mMoreServiceTV[3] = (TextView) findViewById(R.id.more_service_tv4);
        mMoreServiceTV[4] = (TextView) findViewById(R.id.more_service_tv5);
        mMoreServiceTV[5] = (TextView) findViewById(R.id.more_service_tv6);
        mMoreServiceTV[6] = (TextView) findViewById(R.id.more_service_tv7);
        mMoreServiceTV[7] = (TextView) findViewById(R.id.more_service_tv8);
        mMoreServiceTV[8] = (TextView) findViewById(R.id.more_service_tv9);
        mMoreServiceTV[9] = (TextView) findViewById(R.id.more_service_tv10);

        mMoreServiceView[0] = (View) findViewById(R.id.more_service_view1);
        mMoreServiceView[1] = (View) findViewById(R.id.more_service_view2);
        mMoreServiceView[2] = (View) findViewById(R.id.more_service_view3);
        mMoreServiceView[3] = (View) findViewById(R.id.more_service_view4);
        mMoreServiceView[4] = (View) findViewById(R.id.more_service_view5);
        mMoreServiceView[5] = (View) findViewById(R.id.more_service_view6);
        mMoreServiceView[6] = (View) findViewById(R.id.more_service_view7);
        mMoreServiceView[7] = (View) findViewById(R.id.more_service_view8);
        mMoreServiceView[8] = (View) findViewById(R.id.more_service_view9);
        mMoreServiceView[9] = (View) findViewById(R.id.more_service_view10);
        for (int i = 0; i < 10; i++) {
            mMoreServiceBtn[i].setVisibility(View.GONE);
        }

        //从远程服务器返回要显示的数据。可以动态控制显数量，不要超过10个
        for (int i = 0; i < 5; i++) {
            mMoreServiceBtn[i]
                    .setVisibility(View.VISIBLE);
//			mMoreServiceTV[i].setText(mList.get(i)
//					.get("title").toString());
//			mMoreServiceTV[i].setTag(mList.get(i)
//					.get("id").toString());
        }


        SetButtonType(0);

    }

    private void CloseButton() {
        for (int i = 0; i < 10; i++) {
            mMoreServiceBtn[i].setBackgroundResource(R.color.gray_e1);
            mMoreServiceTV[i].setTextColor(0xff7d7d7d);
            mMoreServiceView[i].setVisibility(View.GONE);
        }
    }

    private void SetButtonType(int sum) {
        CloseButton();

        mMoreServiceBtn[sum].setBackgroundResource(R.color.white);
        mMoreServiceTV[sum].setTextColor(0xff20bf1e);
        mMoreServiceView[sum].setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_service_btn1:
                SetButtonType(0);
                break;
            case R.id.more_service_btn2:
                SetButtonType(1);
                break;
            case R.id.more_service_btn3:
                SetButtonType(2);
                break;
            case R.id.more_service_btn4:
                SetButtonType(3);
                break;
            case R.id.more_service_btn5:
                SetButtonType(4);
                break;
            case R.id.more_service_btn6:
                SetButtonType(5);
                break;
        }

    }


    /**
     * 下拉刷新
     *
     * @param index 当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        //list.clear();
        //pageNo = 1;

        mSwipyRefreshLayout.setRefreshing(false);
    }

    /**
     * 加载更多
     *
     * @param index 当前分页索引
     */
    @Override
    public void onLoad(int index) {
        mSwipyRefreshLayout.setRefreshing(false);
    }

}
